﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class UserNameDAO
    {
        public static string LayPassword(UserNameDTO usDto)
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            string query = String.Format("select Password from USER_NAME where UserName = '{0}'", usDto.UserName);

            SqlCommand cmd = new SqlCommand(query, cnn);

            cnn.Open();
            try
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    return (string)dr["Password"];
                }
                else 
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
        }

        public static void DoiMatKhau(UserNameDTO usDto)
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            string query = String.Format("update USER_NAME set Password = @Password where UserName = '{0}'", usDto.UserName);
            SqlCommand cmd = new SqlCommand(query, cnn);

            cmd.Parameters.Add("@Password", SqlDbType.Char);
            cmd.Parameters["@Password"].Value = usDto.Password;

            cnn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
        }
    }
}
